import { Component, OnInit, Host } from "@angular/core";
import { AppComponent } from "../../../../app.component";
import { UsersService } from "../users.service";
import { ActivatedRoute } from "@angular/router";
import { ToasterService } from "angular2-toaster";
import { UserModel } from "../../models/user.model";
import swal, { SweetAlertOptions } from "sweetalert2";

@Component({
    templateUrl: './user.component.html',
    providers: [UsersService]
})
export class UserComponent implements OnInit {
    public user: UserModel;

    constructor(@Host() private rootComponent: AppComponent,
    private route: ActivatedRoute,
    private userService: UsersService,
    private toasterService: ToasterService){
    }

    ngOnInit() {
        this.route.params.subscribe(param => {
            this.userService.getUser(param['id']).subscribe(data => {
                this.user = data;
                this.rootComponent.finishLoading();
            });
        });
    }

    public resetPass(){
        const swalOptions: SweetAlertOptions = {
            buttonsStyling: false,
            customClass: 'modal-content',
            showCancelButton: true,
            title: 'Reset Password',
            text: "Are you sure that you want to reset the user's password?",
            confirmButtonClass: 'btn btn-primary',
            confirmButtonText: 'Reset',
            cancelButtonClass: 'btn btn btn-outline-dark-grey'
        };
        swal(swalOptions).then(result => {
            if (result && result.value)
                this.userService.resetPass([this.user.id]).subscribe(_ => {
                    this.toasterService.pop('success', 'Success', 'Password reset successfull.');
                });
        });
    }
}